# - Find NetCDF
# Find the native NetCDF includes and library
#
#  NETCDF_INCLUDES    - where to find netcdf.h, etc
#  NETCDF_LIBRARIES   - Link these libraries when using NetCDF
#  NETCDF_FOUND       - True if NetCDF found including required interfaces (see below)
#
# Your package can require certain interfaces to be FOUND by setting these
#
#  NETCDF_CXX         - require the C++ interface and link the C++ library
#  NETCDF_F77         - require the F77 interface and link the fortran library
#  NETCDF_F90         - require the F90 interface and link the fortran library
#
# The following are not for general use and are included in
# NETCDF_LIBRARIES if the corresponding option above is set.
#
#  NETCDF_LIBRARIES_C    - Just the C interface
#  NETCDF_LIBRARIES_CXX  - C++ interface, if available
#  NETCDF_LIBRARIES_F77  - Fortran 77 interface, if available
#  NETCDF_LIBRARIES_F90  - Fortran 90 interface, if available
#
# Normal usage would be:
#  set (NETCDF_F90 "YES")
#  find_package (NetCDF REQUIRED)
#  target_link_libraries (uses_f90_interface ${NETCDF_LIBRARIES})
#  target_link_libraries (only_uses_c_interface ${NETCDF_LIBRARIES_C})

if (NETCDF_INCLUDES AND NETCDF_LIBRARIES)
  # Already in cache, be silent
  set (NETCDF_FIND_QUIETLY TRUE)
endif (NETCDF_INCLUDES AND NETCDF_LIBRARIES)

find_path (NETCDF_INCLUDES netcdf.h HINTS NETCDF_DIR "$ENV{NETCDF}/include" ENV NETCDF_DIR ENV MPI_INCLUDE)

find_path (NETCDF_MODULES netcdf.mod HINTS NETCDF_DIR "$ENV{NETCDF}/mod" ENV NETCDF_MOD ENV MPI_FORTRAN_MOD_DIR "/usr/lib64/gfortran/modules")

find_library(NETCDF_LIBRARIES NAMES netcdff PATHS "$ENV{NETCDF}" PATH_SUFFIXES lib lib64 ENV NETCDF_LIB ENV MPI_LIB)

find_library (NETCDF_LIBRARIES_C NAMES netcdf PATHS "$ENV{NETCDF}" PATH_SUFFIXES lib lib64 ENV NETCDF_LIB ENV MPI_LIB)

mark_as_advanced(NETCDF_LIBRARIES_C)

set (NetCDF_has_interfaces "YES") # will be set to NO if we're missing any interfaces
set (NetCDF_libs "${NETCDF_LIBRARIES_C}")

get_filename_component(NETCDF_LIB_DIR "${NETCDF_LIBRARIES}" DIRECTORY)
get_filename_component(NETCDF_LIB_DIR_C "${NETCDF_LIBRARIES_C}" DIRECTORY)

macro (NetCDF_check_interface lang header libs)
  if (NETCDF_${lang})
    find_path (NETCDF_INCLUDES_${lang} NAMES ${header}
      HINTS "${NETCDF_INCLUDES}" "${NETCDF_MODULES}" NO_DEFAULT_PATH)
    find_library (NETCDF_LIBRARIES_${lang} NAMES ${libs}
      HINTS "${NETCDF_LIB_DIR}" "${NETCDF_LIB_DIR_C}" NO_DEFAULT_PATH)
    mark_as_advanced (NETCDF_INCLUDES_${lang} NETCDF_LIBRARIES_${lang})
    if (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
      list (INSERT NetCDF_libs 0 ${NETCDF_LIBRARIES_${lang}}) # prepend so that -lnetcdf is last
    else (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
      set (NetCDF_has_interfaces "NO")
      message (STATUS "Failed to find NetCDF interface for ${lang}")
    endif (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
  endif (NETCDF_${lang})
endmacro (NetCDF_check_interface)

NetCDF_check_interface (CXX netcdfcpp.h netcdf_c++)
NetCDF_check_interface (F77 netcdf.inc  netcdff)
NetCDF_check_interface (F90 netcdf.mod  netcdff)

# Add links to library dependencies (e.g. -lhdf5 -lm -lz )
find_program (NETCDF_CONFIG_EXE NAMES nc-config PATHS "$ENV{NETCDF}/bin" ENV NETCDF_DIR)
if (NETCDF_CONFIG_EXE)
  execute_process( COMMAND ${NETCDF_CONFIG_EXE} --libs RESULT_VARIABLE nc_config_ret OUTPUT_VARIABLE nc_config_libs)
  if (nc_config_ret EQUAL 0)
    string( STRIP ${nc_config_libs} nc_config_libs )
    list (APPEND NetCDF_libs ${nc_config_libs})
    list (REMOVE_DUPLICATES NetCDF_libs)
  else (nc_config_ret EQUAL 0)
    message(WARNING "nc-config --libs not found, library dependencies may not link.")
  endif (nc_config_ret EQUAL 0)
else (NETCDF_CONFIG_EXE)
  message(WARNING "nc-config not found, library dependencies may not link.")
endif (NETCDF_CONFIG_EXE)

set (NETCDF_LIBRARIES ${NetCDF_libs} CACHE INTERNAL "All NetCDF libraries required for interface level")

# handle the QUIETLY and REQUIRED arguments and set NETCDF_FOUND to TRUE if
# all listed variables are TRUE
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (NetCDF DEFAULT_MSG NETCDF_LIBRARIES NETCDF_INCLUDES NetCDF_has_interfaces)

if (NETCDF_FOUND AND NOT TARGET netCDF::netcdf)
  add_library(netCDF::netcdf UNKNOWN IMPORTED)
  set_target_properties(
    netCDF::netcdf
    PROPERTIES
    IMPORTED_LOCATION "${NETCDF_LIBRARIES_C}"
    IMPORTED_LINK_INTERFACE_LANGUAGES C
    INTERFACE_INCLUDE_DIRECTORIES "${NETCDF_INCLUDES}"
    )
endif()
if (NETCDF_FOUND AND NOT TARGET netCDF::netcdff)
  add_library(netCDF::netcdff UNKNOWN IMPORTED)
  set_target_properties(
    netCDF::netcdff
    PROPERTIES
    IMPORTED_LOCATION "${NETCDF_LIBRARIES_F90}"
    IMPORTED_LINK_INTERFACE_LANGUAGES Fortran
    INTERFACE_INCLUDE_DIRECTORIES "${NETCDF_MODULES}"
    )
  target_link_libraries(netCDF::netcdff INTERFACE netCDF::netcdf)
endif()

mark_as_advanced (NETCDF_LIBRARIES NETCDF_INCLUDES)
